package com.ciaojian.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ciaojian.client.service.TimesCardServiceImpl;
import com.ciaojian.client.service.StoreServiceImpl;
import com.ciaojian.core.mapper.StoreMapper;
import com.ciaojian.core.model.ProductGroup;
import com.ciaojian.core.model.Store;
import com.ciaojian.core.util.DistanceUtils;
import com.ciaojian.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;

/**
 * @author Atlakyn
 */
@Api(tags = "门店模块: 门店")
@RestController
@RequestMapping("/orders")
public class StoreController {
    @Resource
    private StoreServiceImpl storeService;
    @Resource
    private TimesCardServiceImpl timesCardService;
    @Resource
    private StoreMapper storeMapper;

    @ApiOperation("获取门店次卡")
    @GetMapping("/{storeId}/times-card")
    public Result getTimesCard(@PathVariable Integer storeId){
        List<ProductGroup> productGroup = timesCardService.getProductGroup(storeId);
        return Result.ok(productGroup);
    }

    /**
     * 获取所有店
     *
     * @param lng 经度
     * @param lat 纬度
     * @return
     */
    @GetMapping("storeList")
    public Result getStoreList(String city, String name, String lat, String lng,@RequestParam(value = "type",defaultValue = "0") int type) {
        if (lat == null || lng == null) {
            return Result.error("经纬度不能为空");
        }
        QueryWrapper<Store> qw = new QueryWrapper<>();
        if (name != null && !"".equals(name)) {
            qw.and(wrapper -> wrapper.like("title", name).or().like("city", name).or().like("province", name));
        }
        if (city != null && !"".equals(city)) {
            qw.like("city", city);
        }
        if (type != 0) {

            qw.eq("type", type);
        }
        qw.eq("is_hidden", 0);
        qw.eq("business_type", 0);
        qw.eq("is_del", 0);
        IPage storeList = storeService.page(new Page<>(0, 100), qw);
        List<Store> records = storeList.getRecords();
        for (Store data : records) {
            // 查询门店外面的排队人数
            Integer num = storeService.getBaseMapper().getStoreNum(data.getId() + "");
            double d = DistanceUtils.getDistance(
                    data.getLng(),
                    data.getLat(),
                    Double.parseDouble(lng),
                    Double.parseDouble(lat)
            );
            BigDecimal b = new BigDecimal(d);
            if (b.intValue() < 1) {
                Double jl = b.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
                data.setMiter(jl);
            } else {
                Double jl = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
                data.setMiter(jl);
            }
            data.setNum(num);
        }
        records.sort(Comparator.comparingDouble(Store::getMiter));
        return Result.ok(storeList);
    }

    @ApiOperation("获取门店信息")
    @GetMapping("/{storeId}")
    public Result getStore(@PathVariable Integer storeId){
        Store store = storeMapper.selectById(storeId);
        store.setPayKey(null);
        store.setMchId(null);
        store.setFirst(null);
        store.setFirstDay(null);
        store.setFirstMoney(null);

        return Result.ok(store);
    }
}
